package org.cid54.jshopper.controller;

import org.cid54.jshopper.model.ShoppingList;
import org.cid54.jshopper.model.UserGroup;
import org.cid54.jshopper.service.ShoppingListService;
import org.cid54.jshopper.service.UserAndGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author fgo
 */
@Controller
@RequestMapping("shoppingList")
@SessionAttributes({"userName"})
public class ShoppingListController {

    @Autowired
    private ShoppingListService shoppingListService;
    @Autowired
    private UserAndGroupService userAndGroupService;

    @RequestMapping(method = RequestMethod.GET, value = "{shoppingListId}")
    public ModelAndView doGet(@PathVariable("shoppingListId") long shoppingListId) {

        ModelAndView mav = new ModelAndView("shoppingList");

        ShoppingList list = this.shoppingListService.getShoppingListById(shoppingListId);

        mav.addObject("shoppingList", list);

        return mav;

    }

    @ModelAttribute("shoppingList")
    public ShoppingList getNewShoppingList() {
        ShoppingList result = new ShoppingList();
        result.setUserGroup(new UserGroup());
        return result;
    }

    @RequestMapping(method = RequestMethod.GET, value = "add")
    public ModelAndView doAddGet(@ModelAttribute("userName") String userName) {

        ModelAndView mav = new ModelAndView("shoppingListCU");

        mav.addObject("btnAction", "Save");

        mav.addObject("userGroups", this.userAndGroupService.getUserGroupsByUserName(userName));

        mav.addObject("action", "add");

        return mav;

    }

    @RequestMapping(method = RequestMethod.POST, value = "add")
    public String doAddPost(ShoppingList list) {

        this.shoppingListService.create(list);

        return "redirect:/";

    }

    @RequestMapping(method = RequestMethod.GET, value = "edit/{shoppingListId}")
    public ModelAndView doEditGet(@PathVariable("shoppingListId") long shoppingListId, @ModelAttribute("userName") String userName) {

        ModelAndView mav = new ModelAndView("shoppingListCU");

        ShoppingList sl = this.shoppingListService.getShoppingListById(shoppingListId);

        mav.addObject("shoppingList", sl);

        mav.addObject("userGroups", this.userAndGroupService.getUserGroupsByUserName(userName));

        mav.addObject("btnAction", "Update");

        mav.addObject("action", shoppingListId);

        return mav;

    }

    @RequestMapping(method = RequestMethod.POST, value = "edit/{shoppingListId}")
    public String doEditPost(@PathVariable("shoppingListId") long shoppingListId, ShoppingList list) {

        this.shoppingListService.update(shoppingListId, list);

        return "redirect:/";

    }

    @RequestMapping(method = RequestMethod.GET, value = "delete/{shoppingListId}")
    public String doDeletePost(@PathVariable("shoppingListId") long shoppingListId) {

        this.shoppingListService.delete(shoppingListId);

        return "redirect:/mvc/home";

    }

    @RequestMapping(method = RequestMethod.GET, value = "{shoppingListId}/deleteItem/{itemId}")
    public String doDeleteShopItemPost(@PathVariable("shoppingListId") long shoppingListId, @PathVariable("itemId") long itemId) {

        this.shoppingListService.deleteShopItem(shoppingListId, itemId);

        return "redirect:/mvc/shoppingList/" + shoppingListId;

    }
}
